<?php
/**
 * 商家入住登录
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-04-22 15:44:32
 * @version $Id$
 */
namespace app\bis\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{
    // 登录
    public function index(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            // 提交校验
            $vali = $this->validate($data, 'Login');
            if (TRUE !== $vali) {
                $this->error('帐号或者密码错误，请检查后重新提交！');
            }
            $db = model('BisAccount');
            $res = $db->field('id,username,password,code,bis_id,status')->where(['username'=>$data['username']])->find();
            if (!$res || $res['status'] !== 1) {
                $this->error('该用户不存在，获取用户未被审核通过！');
            }
            $pw = md5($data['password'].$res['code']);
            if ($res['password']!=$pw) {
                $this->error('密码错误！');
            }
            $db->update(
                [
                  'last_login_ip'=>$request->ip(),
                  'last_login_time'=>time(),
                ],
                ['id'=>$res['id']]
            );
            // 保存用户信息，作用域bis
            $data = [
                'id' => $res->id,
                'username' => $res->username,
                'password' => $res->password,
                'code' => $res->code,
                'bis_id' => $res->bis_id,
                'status' => $res->status,
            ];
            session('bisAccount',$data,'bis');
            // 重定向跳转
            $this->success('登录成功！','index/index');
        }
        if (session('?bisAccount','','bis')) {
            $this->redirect('index/index');
        }
        return $this->fetch();
    }

    // 退出
    public function logout()
    {
        session(null, 'bis');
        $this->redirect('login/index');
    }

}
